﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using GBL.Repository.Resources;

namespace GBlason.ViewModel.CoatOfArm
{
    /// <summary>
    /// The handler for the fur tincture (special because it's a drawing brush, not a solidcolorbrush)
    /// </summary>
    public class FurViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// Gets or sets the type of fur.
        /// </summary>
        /// <value>
        /// The type fur.
        /// </value>
        public Furs TypeFur { get; set; }

        private String _mainShapeGeometry;
        /// <summary>
        /// Gets or sets the main shape geometry.
        /// </summary>
        /// <value>
        /// The main shape geometry.
        /// </value>
        public String MainShapeGeometry
        {
            get { return _mainShapeGeometry; }
            set
            {
                if (_mainShapeGeometry == value) return;
                _mainShapeGeometry = value;
                var geo = Geometry.Parse(_mainShapeGeometry);
                MainShapeSize = new Size(geo.Bounds.TopRight.X, geo.Bounds.BottomRight.Y);
                OnPropertyChanged("MainShapeGeometry");
            }
        }

        /// <summary>
        /// Gets or sets the color of the main shape.
        /// </summary>
        /// <value>
        /// The color of the main shape.
        /// </value>
        public Color MainShapeColor { get; set; }

        /// <summary>
        /// Gets or sets the color of the background.
        /// </summary>
        /// <value>
        /// The color of the background.
        /// </value>
        public Color BackgroundColor { get; set; }

        private Size _mainShapeSize;
        /// <summary>
        /// Gets or sets the size of the main shape.
        /// </summary>
        /// <value>
        /// The size of the main shape.
        /// </value>
        public Size MainShapeSize
        {
            get { return _mainShapeSize; }
            set
            {
                if (_mainShapeSize == value)
                    return;
                _mainShapeSize = value;
                OnPropertyChanged("MainShapeSize");
            }
        }


        /// <summary>
        /// Create and return this class resulting brush.
        /// By default every tile is absolute sized, and the stretch set to fill.
        /// If the parameter is true then the tile is 
        /// </summary>
        /// <param name="onlyOneForSmallDisplay">if set to <c>true</c> [only one for small display].</param>
        /// <returns></returns>
        public DrawingBrush ResultingBrush(bool onlyOneForSmallDisplay = false)
        {
            var bMode = onlyOneForSmallDisplay ? BrushMappingMode.RelativeToBoundingBox : BrushMappingMode.Absolute;
            var vPort = onlyOneForSmallDisplay ? new Rect(0, 0, 1, 1) : new Rect(MainShapeSize);
            return new DrawingBrush()
            {
                TileMode = TileMode.Tile,
                Stretch = Stretch.Fill,
                ViewportUnits = bMode,
                Viewport = vPort,
                Drawing = new DrawingGroup
                {
                    Children = new DrawingCollection
                    {
                        new GeometryDrawing
                        {
                            Brush = new SolidColorBrush(BackgroundColor),
                            Geometry = new RectangleGeometry{ Rect = new Rect(new Point(0,0), MainShapeSize) }
                        },
                        new GeometryDrawing
                        {
                            Brush = new SolidColorBrush(MainShapeColor),
                            Geometry= Geometry.Parse(MainShapeGeometry)
                        }
                    }
                }
            };
        }

        #region INotifyPropertyChanged Members

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        //Fill avec Viwport absolute and 0,0,sizeX,sizeY with the size of the main shape
        //the background is the same, in absolute, and no more problem ! have to check the output result though

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
